<template>
  <div>
    <div class="slot">
      <div class="top">
        <div class="return" @click="goback">
          <img src="@/assets/my/zuo.png" alt />
        </div>
        <span>我的订单</span>
      </div>
    </div>
    <div class="cd_top">
      <div class="lateral-sliding">
        <div
          class="lateral-sliding-item"
          v-for="(item, index) in statelist"
          :key="index"
          @click="changeactive(item.id)"
        >
          <div :class="active == item.id ? 'li1' : 'li'">{{ item.name }}</div>
          <div :class="active == item.id ? 'span1' : ''"></div>
        </div>
      </div>
    </div>
    <div class="main">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list
          :finished="finished"
          @load="onLoad"
          v-model="loading"
          finished-text="暂无数据了~"
          :immediate-check="false"
        >
          <div class="main_center" ref="main_center">
            <div class="center_li" v-for="(item, index) in list" :key="index">
              <div class="li_main">
                <div class="li_top">
                  <div class="top_left">
                    订单号：
                    <span>{{ item.order_no }}</span>
                  </div>
                  <div class="top_right" v-if="item.status == 3">待发货</div>
                  <div class="top_right" v-if="item.status == 8">已出单</div>
                  <div class="top_right" v-if="item.status == 4">已发货</div>
                  <div class="top_right" v-if="item.status == 2">待自提</div>
                  <!-- <div class="top_right" v-if="item.status == 4">待收货</div> -->
                  <div class="top_right" v-if="item.status == 5">已完成</div>
                  <div class="top_right" v-if="item.status == 6">售后中</div>
                  <div class="top_right" v-if="item.status == 7">已取消</div>
                  <div class="top_right" v-if="item.status == 1">待付款</div>
                  <div class="top_right" v-if="item.status == 9">取消申请中</div>
                </div>
                <div class="li_cent" @click="orderinfo(item.id)" v-for="(item1,index1) in item.goods" :key="index1">
                <!-- <div class="li_cent" @click="orderinfo(item.id)"> -->
                  <div class="img">
                    <img :src="item1.image" alt />
                  </div>
                  <div class="shop_info">
                    <div class="shop_name">{{ item1.title }}</div>
                    <div class="shop_price">
                      ￥
                      <span>{{ item1.o_price }}/{{item1.unit}}</span>
                    </div>
                    <div class="cd_info">
                      <div class="shop_spec">
                        已选：{{ item1.specs_name }} {{ item1.num }}{{item1.unit}}
                      </div>
                    </div>
                     <div class="cd_info">
                      <div class="all_price" v-if="index1==(item.goods.length-1)">
                        合计：
                        <span>￥{{ item.total }}</span>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="btn">
                  <div class="btn_left">
                    <div v-if="item.status == 2">
                      取货码:
                      <span>{{ item.delivery_code }}</span>
                    </div>
                  </div>
                  <div class="btn_right">
                    <div
                      class="cd_btn1"
                      v-if="item.status == 7 || item.status == 3"
                      @click="shanchu(2, item.id)"
                    >
                      删除订单
                    </div>
                    <div
                      class="cd_btn1"
                      v-show="item.status < 4 || item.status == 8"
                      @click="quxiao(1, item.id)"
                    >
                      取消订单
                    </div>
                    <div
                      class="cd_btn1"
                      v-if="item.status == 4"
                      @click="shouhoubuhuo(item.id)"
                    >
                      售后补货
                    </div>
                    <div
                      class="cd_btn1"
                      v-show="item.status === 6"
                      @click="quxiaosh(6, item.id)"
                    >
                      取消售后
                    </div>
                    <div
                      class="cd_btn2"
                      v-if="
                        item.status == 3 ||
                        item.status == 4 ||
                        item.status == 6 ||
                        item.status == 5 ||
                        item.status == 8
                      "
                      @click="orderinfo(item.id)"
                    >
                      查看详情
                    </div>
                    <div
                      class="cd_btn2"
                      v-if="item.status == 4 || item.status == 2"
                      @click="turnshouhuo(3, item.id)"
                    >
                      确认收货
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <!-- <div class="center_li">
              <div class="li_main">
                <div class="li_top">
                  <div class="top_left">
                    订单号：
                    <span>2020090710003562</span>
                  </div>
                  <div class="top_right">待发货</div>
                </div>
                <div class="li_cent">
                  <div class="img">
                    <img src alt />
                  </div>
                  <div class="shop_info">
                    <div class="shop_name">鲜活生蚝1kg/袋</div>
                    <div class="shop_price">
                      ￥
                      <span>19.90/袋</span>
                    </div>
                    <div class="cd_info">
                      <div class="shop_spec">已选：规格一、100份</div>
                      <div class="all_price">
                        合计：
                        <span>￥1900.00</span>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="btn">
                  <div class="btn_left">
                    取货码:
                    <span>657425</span>
                  </div>
                  <div class="btn_right">
                    <div class="cd_btn">取消订单</div>
                  </div>
                </div>
              </div>
            </div> -->
            
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
    <div class="zhezhao" v-if="isshow"></div>
    <div class="zhezhao1" v-if="isshow">
      <div class="tankuang">
        <div class="guanb" @click="guanbi">
          <img src="@/assets/pblic/guanbi.png" alt />
        </div>
        <div class="p" v-if="type == 2">确定删除该订单？</div>
        <div class="p" v-if="type == 1">确定取消该订单？</div>
        <div class="p" v-if="type == 3">确定收货？</div>
        <div class="btn">
          <div class="btn1" @click="turnquxiao">取消</div>
          <div class="btn2" @click="turn">确定</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      active: 0,
      list: [],
      loading: false, //是否在上拉加载
      isLoading: false, //是否在刷新
      finished: false, //是否加载完成
      page: 1,
      total: 0,
      raw: 20,
      type: 1,
      orderid: 0,
      isshow: false,
      statelist: [
        {
          id: 0,
          name: "全部",
        },
        {
          id: 3,
          name: "待发货",
        },
        // {
        //   id: 4,
        //   name: "待收货",
        // },
        {
          id: 8,
          name: "已出单",
        },
        {
          id: 4,
          name: "已发货",
        },
        {
          id: 2,
          name: "待自提",
        },
        {
          id: 6,
          name: "售后中",
        },
        {
          id: 5,
          name: "已完成",
        },
      ],
    };
  },
  mounted() {
    this.token = window.localStorage.getItem("token");
    if (this.$route.query.state) {
      this.active = this.$route.query.state;
    }
    this.onRefresh();
    var winHeight = document.documentElement.clientHeight; //视口大小
    this.$refs.main_center.style.minHeight = winHeight - 82 + "px"; //调整上拉加载框高度
  },
  methods: {
    goback() {
      this.$router.push("/my");
    },
    changeactive(active) {
      this.active = active;
      this.onRefresh();
    },
    // 下拉刷新
    onRefresh() {
      this.page = 1;
      this.finished = false;
      this.getList();
    },
    //上拉加载
    onLoad() {
      // 异步更新数据
      console.log("加载");
      this.page++;
      axios
        .post(
          "/index/order/order_list",
          this.$qs.stringify({
            token: this.token,
            page: this.page,
            status: this.active,
          })
        )
        .then((res) => {
          var list1 = res.data.data.data;
          this.total = res.data.data.total;
          this.list = [...this.list, ...list1];
          // 加载状态结束
          this.loading = false;
          // 数据全部加载完成
          if (this.list.length >= this.total) {
            this.finished = true;
          }
        });
    },
    getList() {
      axios
        .post(
          "/index/order/order_list",
          this.$qs.stringify({
            token: this.token,
            page: this.page,
            status: this.active,
          })
        )
        .then((res) => {
          this.list = res.data.data.data;
          this.allprice = res.data.data.commission;
          this.total = res.data.data.total;
          this.isLoading = false;
          if (this.list.length >= this.total) {
            this.finished = true;
          }
        });
    },
    // 订单删除
    shanchu(type, id) {
      this.type = type;
      this.orderid = id;
      this.isshow = true;
    },
    quxiao(type, id) {
      this.type = type;
      this.orderid = id;
      this.isshow = true;
    },
    turnquxiao() {
      this.isshow = false;
    },
    guanbi() {
      this.isshow = false;
    },
    turn() {
      if (this.type === 1) {
        axios
          .post(
            "/index/order/cancel",
            this.$qs.stringify({
              token: this.token,
              order_id: this.orderid,
            })
          )
          .then((res) => {
            this.$toast(res.data.msg);
            var list = this.list;
            for (var i = 0; i < list.length; i++) {
              if (list[i].id == this.orderid) {
                list[i].status = 7;
              }
            }
          });
        this.isshow = false;
      } else if (this.type === 2) {
        axios
          .post(
            "/index/order/order_del",
            this.$qs.stringify({
              token: this.token,
              id: this.orderid,
            })
          )
          .then((res) => {
            console.log(res.data);
            if (res.data.code == 1) {
              this.$toast(res.data.msg);
              var list = this.list;
              this.list = list.filter((item) => item.id != this.orderid);
            } else {
              this.$toast(res.data.msg);
            }
          });
        this.isshow = false;
      } else if (this.type === 3) {
        axios
          .post(
            "/index/order/receive",
            this.$qs.stringify({
              token: this.token,
              order_id: this.orderid,
            })
          )
          .then((res) => {
            this.$toast(res.data.msg);
            this.isshow = false;
            var list = this.list;
            for (var i = 0; i < list.length; i++) {
              if (list[i].id == this.orderid) {
                list[i].status = 5;
              }
            }
          });
      }
    },
    quxiaosh(type, orderid) {
      console.log("orderid", orderid);
      axios
        .post(
          "/index/order/after_cancel",
          this.$qs.stringify({
            token: this.token,
            order_id: orderid,
          })
        )
        .then((res) => {
          if (res.data.code === 1) {
            this.$toast(res.data.msg);
          }
        });
    },
    turnshouhuo(type, orderid) {
      this.type = type;
      this.orderid = orderid;
      this.isshow = true;
    },
    orderinfo(id) {
      this.$router.push({
        path: "/orderinfo",
        query: { id, state: this.active },
      });
    },
    shouhoubuhuo(id) {
      this.$router.push({
        path: "/aftereplenish",
        query: { orderid: id, state: this.active },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.slot {
  width: 100%;
  height: 45px;
  .top {
    width: 100%;
    height: 45px;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    align-items: center;
    background: white;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 9;
    .return {
      width: 38px;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      display: flex;
      display: -webkit-flex;
      justify-content: center;
      align-items: center;
      img {
        width: 8px;
        height: 15px;
      }
    }
    span {
      font-size: 18px;
      font-family: PingFang SC;
      font-weight: bold;
      color: #000000;
      line-height: 44px;
    }
  }
}
.cd_top {
  position: fixed;
  top: 45px;
  left: 0;
  z-index: 9;
  overflow-y: hidden;
  background: white;
  border-top: 1px solid #f8f8f8;
  .lateral-sliding {
    height: 45px;
    display: flex;
    display: -webkit-flex;
    align-items: center;
    overflow: auto;
    background: white;
    border-bottom: 1px solid #f8f8f8;
    margin-right: 10px;
    .lateral-sliding-item {
      display: flex;
      display: -webkit-flex;
      align-items: center;
      height: 100%;
      margin-left: 10px;
      position: relative;
      .li {
        width: 42px;
        font-size: 12px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #888888;
        line-height: 12px;
        text-align: center;
      }
      .li1 {
        width: 42px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #000000;
        line-height: 14px;
        text-align: center;
      }
      .span1 {
        width: 28px;
        height: 4px;
        background: #fdd40a;
        border-radius: 2px;
        position: absolute;
        left: 7px;
        bottom: 0;
      }
    }
  }
}

.main {
  width: 375px;
  .main_center {
    width: 375px;
    background: #f8f8f8;
    border-top: 8px solid #f8f8f8;
    padding-top: 45px;
    overflow-y: scroll;
    overflow-x: hidden;
    .center_li {
      width: 100%;
      height: auto;
      background: #ffffff;
      border-bottom: 8px solid #f8f8f8;
      .li_main {
        width: 345px;
        height: auto;
        margin: auto;
        .li_top {
          width: 100%;
          height: 34px;
          display: flex;
          display: -webkit-flex;
          justify-content: space-between;
          align-items: center;
          border-bottom: 1px solid #f8f8f8;
          .top_left {
            font-size: 12px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #9a9a9a;
            line-height: 12px;
            span {
              color: #222222;
            }
          }
          .top_right {
            font-size: 12px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #ff521f;
            line-height: 12px;
          }
        }
        .li_cent {
          width: 100%;
          height: 96px;
          border-bottom: 1px solid #f8f8f8;
          display: flex;
          display: -webkit-flex;
          align-items: center;
          .img {
            width: 75px;
            height: 75px;
            overflow: hidden;
            img {
              width: 100%;
            }
          }
          .shop_info {
            width: 250px;
            margin-left: 30px;
            .shop_name {
              font-size: 14px;
              font-family: PingFang SC;
              font-weight: bold;
              color: #222527;
              line-height: 14px;
            }
            .shop_price {
              font-size: 11px;
              font-family: PingFang SC;
              font-weight: 500;
              color: #9a9a9a;
              line-height: 11px;
              margin-top: 10px;
            }
            .cd_info {
              width: 100%;
              display: flex;
              display: -webkit-flex;
              justify-content: space-between;
              align-items: center;
              margin-top: 17px;
              .shop_spec {
                font-size: 12px;
                font-family: PingFang SC;
                font-weight: 500;
                color: #9a9a9a;
                line-height: 12px;
              }
              .all_price {
                width: 100%;
                text-align: right;
                font-size: 13px;
                font-family: PingFang SC;
                font-weight: 500;
                color: #222222;
                line-height: 13px;
                span {
                  color: #ff521f;
                }
              }
            }
          }
        }
        .btn {
          width: 100%;
          height: 50px;
          display: flex;
          display: -webkit-flex;
          justify-content: space-between;
          align-items: center;
          position: relative;
          .btn_left {
            font-size: 11px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #888888;
            line-height: 11px;
            span {
              font-size: 15px;
              font-weight: bold;
              color: #ff521f;
            }
          }
          .btn_right {
            position: absolute;
            right: 0;
            display: flex;
            display: -webkit-flex;
            .cd_btn1 {
              width: 80px;
              height: 30px;
              border: 1px solid #bfbfbf;
              border-radius: 15px;
              font-size: 14px;
              font-family: PingFang SC;
              font-weight: 500;
              color: #444444;
              line-height: 30px;
              text-align: center;
              margin-left: 10px;
            }
            .cd_btn2 {
              width: 80px;
              height: 30px;
              background: #fdd40a;
              border-radius: 15px;
              font-size: 14px;
              font-family: PingFang SC;
              font-weight: 500;
              color: #222222;
              line-height: 30px;
              text-align: center;
              margin-left: 10px;
            }
          }
        }
      }
    }
  }
}
.zhezhao {
  width: 100%;
  height: 100%;
  background: #000000;
  opacity: 0.5;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 19;
}
.zhezhao1 {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 19;
  display: flex;
  display: -webkit-flex;
  justify-content: center;
  align-items: center;
  .tankuang {
    width: 310px;
    height: 145px;
    background: #ffffff;
    border-radius: 10px;
    position: relative;
    .guanb {
      position: absolute;
      top: 5px;
      right: 5px;
      img {
        width: 18px;
        height: 18px;
      }
    }
    .p {
      width: 100%;
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #000000;
      line-height: 16px;
      text-align: center;
      margin-top: 43px;
    }
    .btn {
      width: 100%;
      display: flex;
      display: -webkit-flex;
      align-items: center;
      justify-content: center;
      margin-top: 30px;
      .btn1 {
        width: 123px;
        height: 37px;
        background: #dddddd;
        border-radius: 18.5px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #555555;
        line-height: 37px;
        text-align: center;
      }
      .btn2 {
        width: 123px;
        height: 37px;
        background: #fdd40a;
        border-radius: 18.5px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #000000;
        line-height: 37px;
        text-align: center;
        margin-left: 24px;
      }
    }
  }
}
</style>